<?php

class Invitation_Search_Lucene extends Zend_Search_Lucene {
	
	/**
	 * @param $directory
	 * @param $create
	 */
	public function __construct($directory, $create) {
		parent::__construct($directory, $create);
	}
	
	public function addDocument(Zend_Search_Lucene_Document $document) {
		$docRef = $document->docRef;
		
		$term = new Zend_Search_Lucene_Index_Term($docRef, 'docRef');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$results = $this->find($query);
		
		if (count($results) > 0) {
			foreach($results as $result) {
				$this->delete($result->id);
			}
		}
		return parent::addDocument($document);
	}
	
	public static function create($directory) {
		return new Zend_Search_Lucene_Proxy(
			new Invitation_Search_Lucene($directory, true)
		);
	}
	
	public static function open($directory) {
		return new Zend_Search_Lucene_Proxy(
			new Invitation_Search_Lucene($directory, false)
		);
	}
}
?>